package com.handong.handserialprogram.module.web;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.afollestad.materialdialogs.MaterialDialog;
import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.KeyConstant;
import com.handong.handserialprogram.module.base.BaseActivity;
import com.handong.handserialprogram.module.base.BasePresenter;

import java.util.HashMap;
import java.util.Map;

public class WebActivity extends BaseActivity {
	
	private WebView wv;
	private String webUrl;

	/*public static void startActivity(Context context, String title, String url) {
		Intent intent = new Intent();
		intent.putExtra("url", url);
		intent.putExtra("title", title);
		intent.setClass(context, WebActivity.class);
		context.startActivity(intent);
		overridePendingTransition(R.anim.push_left_in, R.anim.anim_no);

	}*/

	@Override
	protected int getContentViewID() {
		return R.layout.activity_web;
	}

	@Override
	protected BasePresenter createPresenter() {
		return null;
	}

	@Override
	protected void initViewByID() {
		wv = (WebView) findViewById(R.id.webview_id);
	}

	@Override
	@SuppressLint("JavascriptInterface")
	protected void initViewDataRes() {
		//String title=getIntent().getStringExtra("title");

		webUrl=getIntent().getExtras().getString(KeyConstant.WEB_URL);
		if(TextUtils.isEmpty(webUrl)){
			finish();
			showLongToast("参数异常");
		}
		//initHeaderLayout(false);
		//setCenterTitle(title);
		//webUrl="http://motto.tunnel.qydev.com/rdadmin/resources/html2/ActivityMainPage.html";

		WebSettings settings = wv.getSettings();
		settings.setPluginState(PluginState.ON);
		settings.setJavaScriptEnabled(true);
		settings.setAllowFileAccess(true);
		settings.setDefaultTextEncodingName("UTF-8");
		//settings.setUserAgentString(UserAgent.getUserAgent(this));

		wv.setWebViewClient(new WebViewClient() {
			////在点击请求的是链接是才会调用，重写此方法返回true表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
			public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
				loadurl(view, url);

				return true;
			}
		});
		wv.setWebChromeClient(new WebChromeClient(){
			public void onProgressChanged(WebView view, int progress){
				if(progress==100){
					dismissLoadingDialog();
				}else {
					String msgstr="加载中..."+progress+"%";
					showLoadDialog(msgstr);
				}
			}
		});


		wv.addJavascriptInterface(this, "client");

		wv.requestFocus();
		loadurl(wv, webUrl);
	}




	@Override
	public void onBackPressed() {
		if (wv.canGoBack()) {
			if (wv.getUrl().startsWith(webUrl)) {
				super.onBackPressed();
			} else {
				wv.goBack();
			}
		} else {
			super.onBackPressed();
		}
	}

	public void loadurl(final WebView view, final String url) {
		showLoadDialog("努力加载中");
		runOnUiThread(new Runnable() {
	        public void run() {
				Map<String,String> map=new HashMap<String, String>();
				//String token = SPUtil.getInstance().getToken();
				//if(!Tools.isEmpty(token))map.put("token",token);
				view.loadUrl(url,map);
	        }
	    });
	}
	
	public void onDestroy() {
		wv.stopLoading();
		wv.destroy();
		super.onDestroy();
	}

	/**
	 * 数据加载框
	 */
	private MaterialDialog mLoadingDialog;
	public void showLoadDialog(String content) {
		if (mLoadingDialog == null) {
			mLoadingDialog = new MaterialDialog.Builder(this)
					.progress(true, 0)
					.content(content)
					.canceledOnTouchOutside(false)
					.build();
			mLoadingDialog.show();
		} else {
			mLoadingDialog.setContent(content);
		}
		//ViewUtil.removeAllKeyListener(mLoadingDialog);
	}
	public void dismissLoadingDialog() {
		if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
			mLoadingDialog.dismiss();
		}
	}
}
